package com.yanqu.road.server.logic.thread.platform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.language.LanguageType;
import com.yanqu.road.entity.platform.PlatformUserData;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;

public class PlatformUserDataReportTask extends GeneralTask {
    private Logger logger = LogManager.getLogger(PlatformUserDataReportTask.class.getName());

    private PlatformUserData platformUserData;

    public PlatformUserDataReportTask(PlatformUserData platformUserData) {
        super();
        this.platformUserData = platformUserData;
    }

    @Override
    public void doRun() {
        String url = ChannelConfig.PLATFORM_USER_DATA_REPORT_URL;
        if(StringUtils.isNullOrEmpty(url)){
            return;
        }
        SortedMap<String, String> paramMap = new TreeMap<>();
        String serverMark = "";
        long serverId = GameServer.getInstance().getServerId();
        ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
        if(null != serverInfo){
            serverMark = serverInfo.getMarkStr();
        }
        serverMark = ServerListMgr.getLanguageFixedName(serverMark, LanguageType.PLATFORM_LANGUAGE_ZH_CN);

        paramMap.put("apikey", ChannelConfig.PLATFORM_USER_DATA_REPORT_URL_API_KEY);
        paramMap.put("LEADERBOARD_UPDATE_DAY", "" + platformUserData.getRecordDate());
        paramMap.put("TYPE", "赚速|门客|挚友");
        paramMap.put("SERVER_RANGE", serverMark);
        paramMap.put("SERVER_NAME", ServerListMgr.getServerName(serverId, LanguageType.PLATFORM_LANGUAGE_ZH_CN));
        paramMap.put("USER_ID", "" + platformUserData.getUserId());
        paramMap.put("UID", platformUserData.getPlatformUserId());
        paramMap.put("COMMERCE_NAME", CrossUnionMgr.getUnionName(platformUserData.getUnionUid()));
        paramMap.put("TITLE_ID", "" + platformUserData.getHonoraryTitle());
        paramMap.put("HEAD_SCULPTURE_ID", "" + platformUserData.getHeadIcon());
        paramMap.put("VALUE", platformUserData.getEarnSpeed().toString());
        paramMap.put("DINERS_VALUE", platformUserData.getTotalAbility().toString());
        paramMap.put("FRIEND_VALUE", platformUserData.getTotalIntimacy().toString());
        paramMap.put("USER_NAME", platformUserData.getNickName());
        int respCode = 0;
        int loopTimes = 0;
        do {
            loopTimes++;
            if(loopTimes < 2) {

            } else if(loopTimes < 5){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else if(loopTimes < 10){//尝试太多次了，不要了
//                try {
//                    Thread.sleep(DateHelper.MINUTE_MILLIONS * 5);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                return;
            }else {
                try {
                    Thread.sleep(DateHelper.MINUTE_MILLIONS * 30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                Map<String, String> resultMap = HttpHelper.doGet(url, paramMap);
                if (null == resultMap) {
                    logger.info("PlatformUserDataReport request error");
                } else {
                    String code = resultMap.get("code");
                    String body = resultMap.get("body");
                    if (Objects.equals("200", code) && !StringUtils.isNullOrEmpty(body)) {
                        JSONObject respObj = JSON.parseObject(body);
                        respCode = respObj.getIntValue("code");
                    }
                }
            } catch (Exception e) {
                logger.error("PlatformUserDataReport error:", e);
            }
        }while (respCode != 1);
    }
}
